package com.cip.webapp.action;

import org.acegisecurity.context.SecurityContextHolder;

import com.cip.admin.model.CIPLogger;
import com.cip.admin.model.User;
import com.cip.admin.model.Institute;
import com.cip.admin.service.CIPLoggerService;
import com.cip.admin.service.InstituteService;
import com.cip.admin.service.UserManager;
import com.cip.common.util.SecurityUtil;

import org.acegisecurity.context.SecurityContext;
import org.acegisecurity.context.SecurityContextHolder;
import org.acegisecurity.userdetails.UserDetails;

public class LogoutAction extends BaseAction {
	
	private CIPLoggerService cipLoggerService;
	private UserManager userManager;
	private InstituteService instituteService;

	public String logout() throws Exception {
		SecurityContext securityContext = (SecurityContext) SecurityContextHolder
				.getContext();
		User user = SecurityUtil.getLoggedInUser();
		user.setLoginStatus("Inactive");
		userManager.saveOrUpdateUser(user);
		CIPLogger cipLogger=cipLoggerService.getCIPLoggerById(user.getId());
		cipLogger.setStatus("Inactive");
		cipLoggerService.saveOrUpdateLogger(cipLogger);
		Institute institue=instituteService.getInstituteById(user.getTenantId());
		String prefix="tfks";
		if(null != institue && !institue.getPrefix().isEmpty()){
			prefix=institue.getPrefix();
		}
		SecurityContextHolder.clearContext();
		System.out.println("prefix============"+prefix);
		getRequest().setAttribute("prefix", prefix);
		getResponse().reset();
		return SUCCESS;
	}
	
	
	
	public CIPLoggerService getCipLoggerService() {
		return cipLoggerService;
	}

	public void setCipLoggerService(CIPLoggerService cipLoggerService) {
		this.cipLoggerService = cipLoggerService;
	}

	public UserManager getUserManager() {
		return userManager;
	}

	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}



	public InstituteService getInstituteService() {
		return instituteService;
	}



	public void setInstituteService(InstituteService instituteService) {
		this.instituteService = instituteService;
	}	
}
